package com.jacobson.huobi.components

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.jacobson.core.utils.DeviceUtil
import com.jacobson.core.widget.logger.LoggerFactory
import com.tencent.smtt.export.external.interfaces.IX5WebViewBase


/**
 * ================================================
 *  [RecyclerView]ITEMS之间设置间距
 *  @author Wucz on 2018/7/25 15:16
 * ================================================
 */

internal class SpaceItemDecoration(
        ySpaceInDp: Float = 0f,
        xSpaceInDp: Float = 0f
) : RecyclerView.ItemDecoration() {
    private val ySpace = DeviceUtil.dp2px(ySpaceInDp)
    private val xSpace = if (xSpaceInDp == 0f) DeviceUtil.dp2px(ySpaceInDp) else 0

    init {
        LoggerFactory.getLogger().i("SpaceItemDecoration: xDP = $xSpaceInDp, yDP = $ySpaceInDp, xPX = $xSpace, yPX = $ySpace")
    }

    /**
     * Retrieve any offsets for the given item. Each field of `outRect` specifies
     * the number of pixels that the item view should be inset by, similar to padding or margin.
     * The default implementation sets the bounds of outRect to 0 and returns.
     *
     *
     *
     *
     * If this ItemDecoration does not affect the positioning of item views, it should set
     * all four fields of `outRect` (left, top, right, bottom) to zero
     * before returning.
     *
     *
     *
     *
     * If you need to access Adapter for additional data, you can call
     * [RecyclerView.getChildAdapterPosition] to fetch the adapter position of the
     * View.
     *
     * @param outRect Rect to receive the output.
     * @param view    The child view to decorate
     * @param parent  RecyclerView this ItemDecoration is decorating
     * @param state   The current state of RecyclerView.
     */
    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        super.getItemOffsets(outRect, view, parent, state)
        if (parent.layoutManager is GridLayoutManager) {
            (parent.layoutManager as GridLayoutManager).run {
                if (orientation == GridLayoutManager.VERTICAL) {
                    //纵向
                    outRect.left = xSpace
                    outRect.top = ySpace
                    val position = parent.getChildLayoutPosition(view)
                    if (position < spanCount) {
                        //第一行无TOP
                        outRect.top = 0
                    }
                    if (position % spanCount == 0) {
                        //第一列无LEFT
                        outRect.left = 0
                    }
                }
            }
        } else if (parent.layoutManager is LinearLayoutManager) {
            if (parent.getChildAdapterPosition(view) > 0 && this.ySpace > 0) {
                outRect.top = this.ySpace
            }
        }
    }
}